<?php

namespace app\index\controller;

use think\Db;
use think\Console;
use think\Controller;


class OssTest extends Controller
{
    // 获取临时访问凭证
    public function getTempPlicy()
    {
        $oss = \oss\OssFactory::factory("AliOss");

        $result = $oss->getRamPolicy();

        return $result;
    }

    // 简单上传接口
    public function uploadFile()
    {
        $files = request()->file();
        $input['title'] = input('post.title');
        $uploadFiled = $files['mf'];

        // 获取文件信息
        $fileInfo = $uploadFiled->getInfo();
        $oss = \oss\OssFactory::factory("AliOss");

        $result = $oss->upload($uploadFiled, "");

        //$width = getimagesize($imgUrl)[0];
        //$height = getimagesize($imgUrl)[1];

        return $result;
    }

    // 分片上传接口
    public function multipartUpload()
    {
        $files = request()->file();
        $input['title'] = input('post.title');
        $uploadFiled = $files['mf'];

        $oss = \oss\OssFactory::factory("AliOss");

        $result = $oss->multipartUpload();

        return $result;
    }

    // 分段Push
    public function multionPush()
    {
        global $_W;
        global $_GPC;
        $fileArr = $_FILES['mf'];
        $title = $_GPC['title'];

        // 设置预览目录,上传成功的路径
        $previewPath = "../shiping/";
        $ext = pathinfo($fileArr['name'], PATHINFO_EXTENSION);//获取当前上传文件扩展名
        $arrExt = array('3gp','rmvb','flv','wmv','avi','mkv','mp4','mp3','wav',);

        if(!in_array($ext,$arrExt)) {
            exit(json_encode(-1,JSON_UNESCAPED_UNICODE)); //视/音频或采用了不合适的扩展名！
        } else {
            //文件上传到预览目录
            $previewName = 'pre_'.md5(mt_rand(1000,9999)).time().'.'.$ext; //文件重命名
            $previewSrc = $previewPath.$previewName;

            if(move_uploaded_file($fileArr['tmp_name'],$previewSrc)){ // 上传文件操作，上传失败的操作
                exit($previewName);
            } else {
                //上传成功的失败的操作
                exit(json_encode(0,JSON_UNESCAPED_UNICODE));
            }
        }
    }

    // 分页push页面
    public function multionPushView()
    {

    }

    // 使用ffmpeg开始推流
    public function pushLive()
    {
        //$command = "ffmpeg -re -i https://resources.huanhuanhuishou.com/global_source/B7689D4E-ED00-B402-EBD9-4F52C3B97247.mp4 -c copy -f flv \"rtmp://hhbusiness.oss-cn-shenzhen.aliyuncs.com/live/test_rtmp_live?playlistName=iphone8.m3u8&OSSAccessKeyId=LTAIp7VhX2YoazcF&Expires=1685586947&Signature=ovAOELN2anMs1Y%2Fzam4BhhKzZYE%3D\"";
        //exec($command, $output, $return_var);

        //$result = exec('ipconfig', $output);
        //var_dump($output[4]);die;
    }

    // 创建推流通道
    public function putLiveChannel()
    {
        $oss = \oss\OssFactory::factory("AliOss");

        $result = $oss->putLiveChannel('iphone8');

        return $this->jsonData(200, 'ok', $result);
    }

    // 获取推流加签地址
    public function getSignRtmpUrl()
    {
        $oss = \oss\OssFactory::factory("AliOss");

        $params = array('params' => array('playlistName' => 'newest.m3u8'));

        $result = $oss->getSignRtmpUrl($params);

        return $this->jsonData(200, 'ok', $result);
    }

    // 获取推流上传通道历史记录
    public function getLiveChannelHistory()
    {
        $oss = \oss\OssFactory::factory("AliOss");

        //$params = array('params' => array('playlistName' => 'newest.m3u8'));

        $result = $oss->getLiveChannelHistory();

        return $this->jsonData(200, 'ok', $result);
    }

    // 查询推流上传信息
    public function getLiveChannelInfo()
    {
        $oss = \oss\OssFactory::factory("AliOss");

        $result = $oss->getLiveChannelInfo();

        return $this->jsonData(200, 'ok', $result);
    }

    // 查询m3u8的推流上传状态
    public function getLiveChannelStatus()
    {
        $oss = \oss\OssFactory::factory("AliOss");

        $result = $oss->getLiveChannelStatus();

        return $this->jsonData(200, 'ok', $result);
    }

}
